-------------APP_CASH__________________________________
DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_CashTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_CashTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end	
) 
,actappointmentamount as (
	
	select a."AccountId",sum(A."CashTotal") "AppointmentCashTotal",sum(A."CardTotal") "AppointmentCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") appointmentamount
	from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
   case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		from "Receipt" A		
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId" --and  ap."Status" <> 'C'
	join "Provider" pr on pr."ProviderId"= ap."ProviderId" --and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId" 	and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end
	join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
   where  A."Active"<>false and A."RespectiveId" is not null  
	--and coalesce(A."IsRefunded",false) <>true 
		-- and A."CreatedDate"::date = '2022-12-29'
		 and A."ReceiptAreaTypeId"=4
	and	case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
	and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end --and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"	
)

,refundappointmentamount as (
	
	select a."AccountId" ,sum(A."CashTotal") "AppointmentRefCashTotal",sum(A."CardTotal") "AppointmentRefCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") refappointmentamount  from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
          case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
   --and ap."Status" <> 'C'   and ap."Active" =true
	
   where A."Active"<>false and A."RespectiveId" is not null  
	--and coalesce(A."IsRefunded",false) = true 	and
	and A."ReceiptAreaTypeId"=5 and
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"			
)

,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentCashTotal",0) - coalesce(B."AppointmentRefCashTotal",0) as "AppointmentCashTotal" ,
	coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,	
	coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"
	
	
)

select distinct 
sum(coalesce(ap."AppointmentCashTotal",0)) "TotalCash"

from  accountdata a

left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;

--------------APP_CARD________


DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_CardTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_CardTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end	
) 
,actappointmentamount as (
	
	select a."AccountId",sum(A."CashTotal") "AppointmentCashTotal",sum(A."CardTotal") "AppointmentCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") appointmentamount from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
   case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId" --and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId" 		and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end
	join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
		
   where  A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=4 and
	--and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and
		case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
		and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end --and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"	
		
)

,refundappointmentamount as (
	
	select a."AccountId" ,sum(A."CashTotal") "AppointmentRefCashTotal",sum(A."CardTotal") "AppointmentRefCardTotal",sum(A."CashTotal")+ sum(A."CardTotal") refappointmentamount  from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
          case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal"
		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
  -- and ap."Status" <> 'C'   and ap."Active" =true
	
   where A."Active"<>false and A."RespectiveId" is not null  
	--and coalesce(A."IsRefunded",false) = true 	and
	and A."ReceiptAreaTypeId"=5 and
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"	
	
)

,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentCashTotal",0) - coalesce(B."AppointmentRefCashTotal",0) as "AppointmentCashTotal" ,
	coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,	
	coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"
	
	
)

select distinct 
sum(coalesce(ap."AppointmentCardTotal",0)) "TotalCard"

from  accountdata a

left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;
-------------------APP-UPI________________
DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_UpiTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_UpiTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end		
) 
 
,actappointmentamount as (	
	select a."AccountId",sum(A."UpiTotal") "AppointmentUpiTotal"
	 from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UpiTotal"		
	
	from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId" --and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId" 		and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end

	join "Account" PA on PA."ReferenceId"=pr."ProviderId"   and PA."RoleId"=3
   where  A."Active"<>false and A."RespectiveId" is not null and A."ReceiptAreaTypeId"=4 and  
	--and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and
		case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
		and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end --and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"			
)
,refundappointmentamount as (	
	select a."AccountId" ,sum(A."UpiTotal") "AppointmentRefUpiTotal"
	 from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UpiTotal"         		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
  -- and ap."Status" <> 'C'   and ap."Active" =true	
   where A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=5 and 
	--and coalesce(A."IsRefunded",false) = true 	and	
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"		
)
,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentUpiTotal",0) - coalesce(B."AppointmentRefUpiTotal",0) as "AppointmentUpiTotal"
	--coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,	
	--coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" 
	from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"	
)

select distinct 
sum(coalesce(ap."AppointmentUpiTotal",0)) "TotalUpi"

from  accountdata a
left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;
-------------------APP ONLINE-PAYMENT-------------
DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_OnlinePaymentTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_OnlinePaymentTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end	
	
) 
 
,actappointmentamount as (	
	select a."AccountId",sum(A."OnlinePaymentTotal") "AppointmentTotal"
	 from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end  "OnlinePaymentTotal"		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"-- and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"		and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end

	join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
   where  A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=4 and
	--and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and
		case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
		and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end -- and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"			
)
,refundappointmentamount as (	
	select a."AccountId" ,sum(A."OnlinePaymentTotal") "AppointmentRefTotal"
	 from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end  "OnlinePaymentTotal"         		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
  -- and ap."Status" <> 'C'   and ap."Active" =true	
   where A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=5 and 
	--and coalesce(A."IsRefunded",false) = true 	and	
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"		
)
,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentTotal",0) - coalesce(B."AppointmentRefTotal",0) as "AppointmentTotal"
	from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"	
)

select distinct 
sum(coalesce(ap."AppointmentTotal",0)) "TotalOnlinePayment"

from  accountdata a
left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;
-------------------APP-CHEQUE----
DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_ChequeTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_ChequeTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end	
	
) 
 
,actappointmentamount as (	
	select a."AccountId",sum(A."ChequeTotal") "AppointmentChequeTotal"
	 from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal"		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"-- and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId" 		and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end

	join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
   where  A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=4 and 
--	and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and
		case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
		and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end -- and PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"			
)
,refundappointmentamount as (	
	select a."AccountId" ,sum(A."ChequeTotal") "AppointmentRefChequeTotal"
	 from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end  "ChequeTotal"         		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
  -- and ap."Status" <> 'C'   and ap."Active" =true	
   where A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=5 and
	--and coalesce(A."IsRefunded",false) = true 	and	
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"		
)
,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentChequeTotal",0) - coalesce(B."AppointmentRefChequeTotal",0) as "AppointmentTotal"
	from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"	
)

select distinct 
sum(coalesce(ap."AppointmentTotal",0)) "TotalCheque"

from  accountdata a
left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;
----------------------------APP PAYTM_________
DROP FUNCTION IF EXISTS public."widget_OutPatients_OverAll_PaytmTotal"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_OutPatients_OverAll_PaytmTotal"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with accountdata as (
select a."AccountId",a."FullName" "EmployeeName",rol."RoleName"
from "Account" a
join "Role" rol on rol."RoleId" = a."RoleId" and rol."RoleName"<>'Patient'
join "LocationAccountMap" LA on LA."AccountId"=a."AccountId"
where case when "locationId" is null then 1=1 else LA."LocationId"= "locationId" end	
	
) 
 
,actappointmentamount as (	
	select a."AccountId",sum(A."PaytmTotal") "AppointmentPaytmTotal"
	 from (
   select  a."CreatedBy"  "AccountId",   
   case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end  "PaytmTotal"		
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId" --and ap."Active" =true	
	left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId"	and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end
	join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
   where  A."Active"<>false and A."RespectiveId" is not null and A."ReceiptAreaTypeId"=4 and 
	--and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' and
		case when "fromDate" is null then 1=1 else A."CreatedDate"::date = "fromDate" end	
		and case when "locationId" is null then 1=1 else ap."LocationId"= "locationId" end --or PL."LocationId"= "locationId" end
	and case when "referenceId" is null then 1=1 else PA."ReferenceId"= "referenceId" end	
	)a		
	group by  a."AccountId"			
)
,refundappointmentamount as (	
	select a."AccountId" ,sum(A."PaytmTotal") "AppointmentRefPaytmTotal"
	 from (
    select  a."CreatedBy"  "AccountId",
		   case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end  "PaytmTotal"         		
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
   --and ap."Status" <> 'C'   and ap."Active" =true	
   where A."Active"<>false and A."RespectiveId" is not null  and A."ReceiptAreaTypeId"=5 and
	--and coalesce(A."IsRefunded",false) = true 	and	
	case when "fromDate" is null then 1=1 
	else A."CreatedDate"::date = "fromDate" end
	) a
	group by  a."AccountId"		
)
,appointmentamount as (
select  A."AccountId",
	coalesce(A."AppointmentPaytmTotal",0) - coalesce(B."AppointmentRefPaytmTotal",0) as "AppointmentTotal"
	from actappointmentamount A
	left join refundappointmentamount B on A."AccountId"=B."AccountId"	
)

select distinct 
sum(coalesce(ap."AppointmentTotal",0)) "TotalPaytm"

from  accountdata a
left join appointmentamount ap on ap."AccountId"=a."AccountId"
;
end
$BODY$;
----------------------------------
--------------------------APP_FOLLOWUP _COUNT---------------------------------
------------------------------------------
DROP FUNCTION IF EXISTS public."widget_FollowUp_Appointments_Count"(date, integer, integer);

CREATE OR REPLACE FUNCTION public."widget_FollowUp_Appointments_Count"(
	"fromDate" date DEFAULT NULL::date,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Count" bigint) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

select count(*) from "Appointment" 
where "Active" is true and "AppointmentDate"::date="fromDate" and 
	 "FollowUpForAppointmentId" is not null
and case when "referenceId" is null then 1=1 else "ProviderId"= "referenceId" end
and case when "locationId" is null then 1=1 else "LocationId"= "locationId" end;

end

$BODY$;



